<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security" %>

<script>

function openSaveChangesDialog()
{
  afterFieldEdit('System Parameter Value', 'string', 'systemParameter.value', 'false', new Array());
  setTimeout("checkErrorsForSaveChangesDialog()", 100);
}

function afterFieldEditSystemParam(name, javaType, expression, mandatory)
{
  if (javaType == "java.lang.String")
  {
	 javaType = "string";

  }
  else if (javaType == "java.lang.Integer")
  {
	  javaType = "integer";
  }

  afterFieldEdit(name, javaType, expression, mandatory, new Array());
}

function enableSave()
{
  var saveButton = $("#saveButton");
  saveButton.click(function() {saveSystemParameter();});
  saveButton.mouseover(function () {buttonHover(this);});
  saveButton.mouseout(function() {buttonNormal(this);});
  saveButton.addClass("button_input").removeClass("button_inputDisabled");
}

function disableSave()
{
  var saveButton = $("#saveButton");
  saveButton.unbind();
  saveButton.addClass("button_inputDisabled").removeClass("button_input");
}

function saveSystemParameter()
{
  $(parseIdForJQuery("adminCommand.adminRequestedAction")).val("save");
  $('#adminForm').submit();
}

</script>

<form id="adminForm" name="adminForm" method="post" commandName="adminCommand">

  <form:hidden path="adminCommand.adminRequestedAction" id="adminCommand.adminRequestedAction"/>
  <form:hidden path="adminCommand.isNew" id="adminCommand.isNew"/>
  <input type="hidden" id="callStatus" name="callStatus" value=""/>
  
  <tag:errors name="adminCommand"/>

  <table class="adminEdit" style="text-align: center;">
       <tr>
      <th>Label:</th>
  	  <td>
        <spring:bind path="adminCommand.systemParameter.label">${status.value}</spring:bind>
      </td>
    </tr>
    <security:authorize ifAllGranted="ROLE_EDITABLE">
    <tr>
      <th>Value:</th>
  	  <td>
  	    <spring:bind path="adminCommand.systemParameter.value">
  	    <c:choose>
  	    <c:when test="${adminCommand.systemParameter.editable}">
          <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
          <input id="${status.expression}"
                 name="${status.expression}"
                 value="${status.value}"
                 class="largeTextField"
                 maxlength="${adminCommand.systemParameter.maxLength}"
                 onkeyup="fieldEdit('${status.expression}');"
                 onchange="afterFieldEditSystemParam('System Parameter Value', '${adminCommand.systemParameter.javaType}', '${status.expression}', 'false', new Array());"/>
          <div id="${status.expression}_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
  	    </c:when>
  	    <c:otherwise>
          <input type="hidden" id="${status.expression}" name="${status.expression}" value="${status.value}"/>
          ${status.value}
  	    </c:otherwise>
  	    </c:choose>
  	    </spring:bind>
      </td>
  	</tr>
  	    </security:authorize>

  	 <security:authorize ifNotGranted="ROLE_EDITABLE">
  	 <tr>
     <th>Value:</th>
  	 <td>
  	 <spring:bind path="adminCommand.systemParameter.value">
 	 ${status.value}
 	 </spring:bind>
	 </tr>
  	 </security:authorize>

      <tr>
      <th>Max Length:</th>
      <spring:bind path="adminCommand.systemParameter.maxLength">
      <c:choose>
      <c:when test="${adminCommand.systemParameter.maxLength != null && adminCommand.systemParameter.maxLength != ''}">
       <td>${status.value}</td>
      </c:when>
      <c:otherwise><td>n/a</td></c:otherwise>
      </c:choose>
      </spring:bind>



    </tr>
      <tr>
        <th>Mandatory:</th>
        <td>
          <c:choose>
            <c:when test="${adminCommand.systemParameter.mandatory}"><img src="pages/images/green_tick.gif" height="20" width="20"/></c:when>
            <c:otherwise><img src="pages/images/red_cross.gif" height="20" width="20"/></c:otherwise>
          </c:choose>
        </td>
      </tr>
      <tr>
        <th>Editable:</th>
        <td>
          <c:choose>
            <c:when test="${adminCommand.systemParameter.editable}"><img src="pages/images/green_tick.gif" height="20" width="20"/></c:when>
            <c:otherwise><img src="pages/images/red_cross.gif" height="20" width="20"/></c:otherwise>
          </c:choose>
        </td>
      </tr>
  </table>

  <table style="padding-top: 15px; padding-bottom: 15px;">
    <tr>
      <td style="text-align: center;">
        <a id="saveButton" class="button_inputDisabled"><span><spring:message code="button.save"/></span></a>
      </td>
      <td style="text-align: center;">
        <a id="cancelButton" class="button_input" onclick="this.blur(); cancel();" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);"><span><spring:message code="button.cancel"/></span></a>
      </td>
    </tr>
  </table>

  <jsp:include page="/pages/jsp/tiles/admin/hoverConfirmation.jsp"/>

</form>
